package com.ydzt.fanka.server.space.bufferreceiver;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.log4j.Logger;

import com.ydzt.fanka.server.domain.BufferRecord;
import com.ydzt.fanka.server.init.SynchronizedVariable;

public class BufferReceiverPool {
	static Logger logger = Logger.getLogger(BufferReceiverPool.class.getName());
	private static int iQueueSize = 1000;
	private static BufferReceiverPool bufferPool;
	private BlockingQueue<BufferRecord> bufferQueue = new LinkedBlockingQueue<BufferRecord>(iQueueSize);
	private BufferReceiverPool(){}
	
	public synchronized static BufferReceiverPool getInstants(){
		if(null == bufferPool){
			bufferPool = new BufferReceiverPool();
		}
		return bufferPool;
	}
	
	public synchronized BufferRecord getBufferReceiver(){
		return bufferQueue.poll();
	}
	
	public synchronized void addSocketProducer(BufferRecord record){
		try{
			if(bufferQueue.size() < iQueueSize){
				if(!SynchronizedVariable.getInstance().isBuffLock()){
					bufferQueue.add(record);
				}
				else{
					logger.info("锁住队列!");
				}
			}
			else{
				logger.info("buffer队列已满!");
			}
		}
		catch(Exception e){
			logger.info("buffer队列捕获异常:" + e.getMessage());
			logger.error("buffer队列捕获异常:" + e.getMessage());
		}
	}
}